var ApiUrl = "http://" + window.location.host + "/";

function getToken(){
    var token = getCookie("token");
    return token;
}

function login(uri){
    if(isWeiXin()){
        window.location.href = ApiUrl + "wx/getWxToken?uri="+uri;
    }else{

    }
}

/*验证是否微信客户端*/
function isWeiXin(){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
    }else{
        return false;
    }
}

function checkAjaxResult(result){
    var code = result.code;
    if(code == -1){
        if(result.msg == '登录过期，请重新登录'){
            delCookie("token");
        }
        return false;
    }
    return true;
}

function getQueryString(e) {
    var t = new RegExp("(^|&)" + e + "=([^&]*)(&|$)");
    var a = window.location.search.substr(1).match(t);
    if (a != null) return a[2];
    return ""
}

function addCookie(e, t, a) {
    var n = e + "=" + escape(t) + "; path=/";
    if (a > 0) {
        var r = new Date;
        r.setTime(r.getTime() + a * 60 * 1000);
        n = n + ";expires=" + r.toGMTString()
    }
    document.cookie = n
}
function getCookie(e) {
    var t = document.cookie;
    var a = t.split("; ");
    for (var n = 0; n < a.length; n++) {
        var r = a[n].split("=");
        if (r[0] == e) return unescape(r[1])
    }
    return null
}
function delCookie(e) {
    var t = new Date;
    t.setTime(t.getTime() - 1);
    var a = getCookie(e);
    if (a != null) document.cookie = e + "=" + a + "; path=/;expires=" + t.toGMTString()
}


/**
 * 经纬度计算
 */
var EARTH_RADIUS = 6378137.0;    //单位M
var PI = Math.PI;
function getRad(d){
    return d*PI/180.0;
}
function getFlatternDistance(lat1,lng1,lat2,lng2){
    var f = getRad((lat1 + lat2)/2);
    var g = getRad((lat1 - lat2)/2);
    var l = getRad((lng1 - lng2)/2);

    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);

    var s,c,w,r,d,h1,h2;
    var a = EARTH_RADIUS;
    var fl = 1/298.257;

    sg = sg*sg;
    sl = sl*sl;
    sf = sf*sf;

    s = sg*(1-sl) + (1-sf)*sl;
    c = (1-sg)*(1-sl) + sf*sl;

    w = Math.atan(Math.sqrt(s/c));
    r = Math.sqrt(s*c)/w;
    d = 2*w*a;
    h1 = (3*r -1)/2/c;
    h2 = (3*r +1)/2/s;

    return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
}

(function ($) {
    $.fn.prependAppTip = function (options) {
        var defaults = {
            appendEl:this, //将提示插入该元素前面
            logoSrc:'/Public/image/wx/tip-logo.png',
            msg:'打开App，发现更多精彩',
            delayClose:false,//是否需要自动关闭 例：2000 = 2秒
            open:function () {},
            close:function () {}
        }
        var opts = $.extend(defaults,options);
        var _html = "<div class='public-download-tip'>" +
                        "<div class='logo'>" +
                            "<img src='"+ opts.logoSrc +"' alt=''>" +
                            "<span>"+ opts.msg +"</span>" +
                        "</div>"+
                        "<div class='close'>" +
                            "<div class='btn open'>打开</div>" +
                            "<span class='closebtn'>×</span>"
                        "</div>"
                    "</div>";
        $(opts.appendEl).prepend(_html);

        var $el = $('.public-download-tip');

        $el.find('.open').on('click',function () {
            opts.open();
        });

        $el.find('.closebtn').on('click',function () {
            opts.close();
            $el.remove();
        });

        if(opts.delayClose != false){
            setTimeout(function () {
                $el.remove();
            },opts.delayClose);
        }
    };

})(jQuery);
